package com.bree.erp.jsf.view;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.entity.Status;
import com.bree.erp.jsf.datatable.LazyBreeDataModel;
import com.bree.erp.jsf.httpsess.UserSessionData;
import com.bree.erp.jsf.service.wrapper.PurchaseRequisitionWrapperServiceBean;
import com.bree.erp.jsf.view.pr.AbstractPurchaseRequisitionLazyView;
import com.bree.erp.service.PurchaseRequisitionService;
import com.bree.erp.service.PurchaseRequisitionWrapperService;

@ManagedBean(name = "dtOutstandingPurchaseRequisitionLazyView")
@ViewScoped
public class OutstandingPurchaseRequisitionLazyView extends AbstractPurchaseRequisitionLazyView {

	private static final long serialVersionUID = 1L;

	private static Logger LOG = Logger.getLogger(OutstandingPurchaseRequisitionLazyView.class);
	
	@EJB
	private PurchaseRequisitionService purchaseRequisitionService;
	
	@ManagedProperty(value="#{userSession}")
	private UserSessionData userSession;
	
	private LazyDataModel<PurchaseRequisition> lazyModel;

	private PurchaseRequisition selectedItem;

	public void init() {
		LOG.debug("post-construct-init");
		
		Long projectId = userSession.getProjectId();

		String userId = userSession.getLoggedInUser().getEmail();
		
		lazyModel = new LazyBreeDataModel<PurchaseRequisition, PurchaseRequisitionWrapperService, Long>(
				new PurchaseRequisitionWrapperServiceBean(purchaseRequisitionService, userId, projectId, Status.APPROVED, Status.INPROGRESS), 0L);
	}

	public void onRowSelect(SelectEvent event) {
		FacesMessage msg = new FacesMessage("Item Selected",
				((PurchaseRequisition) event.getObject()).getId() + "");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	/**
	 * @return the selectedItem
	 */
	public PurchaseRequisition getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem the selectedItem to set
	 */
	public void setSelectedItem(PurchaseRequisition selectedItem) {
		this.selectedItem = selectedItem;
	}

	/**
	 * @return the lazyModel
	 */
	public LazyDataModel<PurchaseRequisition> getLazyModel() {
		
		init();
		
		return lazyModel;
	}

	/**
	 * @return the userSession
	 */
	public UserSessionData getUserSession() {
		return userSession;
	}

	/**
	 * @param userSession the userSession to set
	 */
	public void setUserSession(UserSessionData userSession) {
		this.userSession = userSession;
	}

	

}
